(function () {
    var ps = 'absolute';
    //食物
    function Food(option) {
        option = option instanceof Object ? option : {};
        //食物属性
        this.width = option.width || 20;
        this.height = option.height || 20;
        this.x = option.x || 0;
        this.y = option.y || 0;
        this.color = option.color || 'green';
        //所有节点数组
        this.elements = []
    }
    
    //渲染食物到页面上
    Food.prototype.render = function(map) {
        var elem = document.createElement('div');
        //随机位置
        this.x = Tools.getRandom(0, map.clientWidth / this.width - 1) * this.width;
        this.y = Tools.getRandom(0, map.clientHeight / this.height - 1) * this.height;
        //设置样式
        elem.style.width = this.width + 'px';
        elem.style.height = this.height + 'px';
        elem.style.position = ps;
        elem.style.left = this.x + 'px';
        elem.style.top = this.y + 'px';
        elem.style.backgroundColor = this.color;

        map.appendChild(elem);
        this.elements.push(elem);
    }

    //删除一个食物节点
    Food.prototype.remove = function(map, i) {
        map.removeChild(this.elements[i]);
        this.elements.splice(i, 1)
    }
    //向外暴露Food函数
    window.Food = Food
})();
